package com.molichuxing.gateway.bff.terminal.convertor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.molichuxing.gateway.bff.terminal.entity.request.modify.DeviceModifyVo;
import com.molichuxing.gateway.bff.terminal.entity.response.*;
import com.molichuxing.services.business.dto.request.modify.StoreDeviceBizModifyDto;
import com.molichuxing.services.business.dto.response.DeviceInfoBizDto;
import com.molichuxing.services.infrastructure.dto.response.DeviceDetailDto;
import com.molichuxing.services.infrastructure.dto.response.DeviceMonitorDto;
import com.molichuxing.services.property.DeviceMonitorStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zhaopenghui
 * @create_date: 2020年04月1日
 **/
public class Convert {

    /*********************************转换开始*************************************************/
    public static List<DeviceListVo> toDeviceListVoList(List<DeviceInfoBizDto> bizDtoList, List<DeviceDetailDto> deviceDetailList) {
        List<DeviceListVo> voList = new ArrayList<>();
        if (bizDtoList != null && bizDtoList.size() > 0) {
            Map<String, DeviceDetailDto> deviceDetailMap = new HashMap<String, DeviceDetailDto>();
            if (deviceDetailList != null && deviceDetailList.size() > 0) {
                deviceDetailMap = deviceDetailList.stream().collect(Collectors.toMap(DeviceDetailDto::getCpu, a -> a, (k1, k2) -> k1));
            }

            for (DeviceInfoBizDto dto : bizDtoList) {
                voList.add(toDeviceListVo(dto, deviceDetailMap));
            }
        }
        return voList;
    }

    public static DeviceListVo toDeviceListVo(DeviceInfoBizDto dto, Map<String, DeviceDetailDto> deviceDetailMap) {
        DeviceListVo vo = new DeviceListVo();
        if (null == dto) {
            return vo;
        }

        BeanUtils.copyProperties(dto, vo);
        String cpu = dto.getCpu();
        DeviceDetailDto deviceDetailDto = deviceDetailMap.get(cpu);
        if (null == deviceDetailDto) {
            return vo;
        }

        BeanUtils.copyProperties(deviceDetailDto, vo);
        String requestData = deviceDetailDto.getRequestData();
        JSONObject jsonObject = JSONObject.parseObject(requestData);
        String netTimeOut = jsonObject.getJSONObject("net_timeout").getString("net_timeout");
        List<String> netTimeOutList = JSON.parseArray(netTimeOut, String.class);
        if (netTimeOutList != null && netTimeOutList.size() > 0) {
            vo.setNetTimeOut(netTimeOutList.get(0));
            vo.setNetTimeOutList(netTimeOutList);
        }
        vo.setLastConnectTime(dto.getHeartTime());
        return vo;
    }

    public static List<DeviceErrorListVo> toDeviceErrorListVoList(List<DeviceInfoBizDto> bizDtoList, List<DeviceDetailDto> deviceDetailList) {
        List<DeviceErrorListVo> voList = new ArrayList<>();
        if (bizDtoList != null && bizDtoList.size() > 0) {
            Map<String, DeviceDetailDto> deviceDetailMap = new HashMap<String, DeviceDetailDto>();
            if (deviceDetailList != null && deviceDetailList.size() > 0) {
                deviceDetailMap = deviceDetailList.stream().collect(Collectors.toMap(DeviceDetailDto::getCpu, a -> a, (k1, k2) -> k1));
            }

            for (DeviceInfoBizDto dto : bizDtoList) {
                voList.add(toDeviceErrorListVo(dto, deviceDetailMap));
            }
        }
        return voList;
    }

    public static DeviceErrorListVo toDeviceErrorListVo(DeviceInfoBizDto dto, Map<String, DeviceDetailDto> deviceDetailMap) {
        DeviceErrorListVo vo = new DeviceErrorListVo();
        if (null == dto) {
            return vo;
        }

        BeanUtils.copyProperties(dto, vo);
        String cpu = dto.getCpu();
        DeviceDetailDto deviceDetailDto = deviceDetailMap.get(cpu);
        if (null == deviceDetailDto) {
            return vo;
        }
        BeanUtils.copyProperties(deviceDetailDto, vo);
        vo.setLastConnectTime(dto.getHeartTime());
        return vo;
    }

    public static List<DeviceAppVo> toDeviceAppVoList(DeviceDetailDto dto) {
        List<DeviceAppVo> voList = new ArrayList<>();
        if (null == dto) {
            return voList;
        }

        String requestData = dto.getRequestData();
        JSONObject jsonObject = JSONObject.parseObject(requestData);
        String netTimeOut = jsonObject.getString("list");
        List<DeviceAppJsonVo> jsonList = JSON.parseArray(netTimeOut, DeviceAppJsonVo.class);
        if (null == jsonList || jsonList.size() < 1) {
            return voList;
        }

        for (DeviceAppJsonVo deviceAppJsonVo : jsonList) {
            DeviceAppVo deviceAppVo = new DeviceAppVo();
            BeanUtils.copyProperties(deviceAppJsonVo, deviceAppVo);
            deviceAppVo.setCpuPer(getString(deviceAppJsonVo.getCpu()));
            deviceAppVo.setMemoryPer(getString(deviceAppJsonVo.getMemoryPer()));
            deviceAppVo.setDiskPer(getString(deviceAppJsonVo.getApkPer()));
            deviceAppVo.setNetPer(getString(deviceAppJsonVo.getNetPer()));
            deviceAppVo.setMemory(getString(deviceAppJsonVo.getMemory()));
            Long installTime = deviceAppJsonVo.getInstallTime();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(installTime), ZoneId.systemDefault());
            deviceAppVo.setInstallTime(localDateTime);
            voList.add(deviceAppVo);
        }
        return voList;
    }

    private static String getString(String str) {
        if (StringUtils.isBlank(str)) {
            return "0";
        }

        DecimalFormat format = new DecimalFormat("0.00");
        BigDecimal bigDecimal = new BigDecimal(str);
        if (bigDecimal.compareTo(new BigDecimal("0.01")) == -1) {
            return "0";
        }
        return format.format(bigDecimal);
    }

    public static DeviceErrorVo toDeviceErrorVo(DeviceInfoBizDto dto, List<DeviceMonitorDto> deviceMonitorList) {
        DeviceErrorVo vo = new DeviceErrorVo();
        if (null == dto) {
            return vo;
        }

        BeanUtils.copyProperties(dto, vo);
        List<DeviceErrorInfoVo> deviceErrorInfoList = toDeviceErrorInfoVoList(deviceMonitorList);
        vo.setDeviceList(deviceErrorInfoList);
        return vo;
    }

    public static List<DeviceErrorInfoVo> toDeviceErrorInfoVoList(List<DeviceMonitorDto> dtoList) {
        List<DeviceErrorInfoVo> voList = null;
        if (dtoList != null && dtoList.size() > 0) {
            voList = new ArrayList<>();
            for (DeviceMonitorDto dto : dtoList) {
                voList.add(toDeviceErrorInfoVo(dto));
            }
        }
        return voList;
    }

    public static DeviceErrorInfoVo toDeviceErrorInfoVo(DeviceMonitorDto dto) {
        DeviceErrorInfoVo vo = null;
        if (dto != null) {
            vo = new DeviceErrorInfoVo();
            BeanUtils.copyProperties(dto, vo);
            vo.setErrorMessage(dto.getErrorMsg());
            Integer status = dto.getStatus();
            if (status != null
                    && !status.equals(DeviceMonitorStatusEnum.NORMAL.getValue())
                    && !status.equals(DeviceMonitorStatusEnum.UNKNOWN.getValue())) {
                status = DeviceMonitorStatusEnum.ERROR.getValue();
            }
            vo.setStatus(status);
            vo.setStatusStr(DeviceMonitorStatusEnum.getName(status));
        }
        return vo;
    }


    public static StoreDeviceBizModifyDto toStoreDeviceBizModifyDto(DeviceModifyVo vo) {
        StoreDeviceBizModifyDto dto = new StoreDeviceBizModifyDto();
        if (null == vo) {
            return dto;
        }

        BeanUtils.copyProperties(vo, dto);
        return dto;
    }

    /********************************* 转换结束 *************************************************/

}
